#include "speedplaybutton.h"
#include <QPainter>
#include "csppedplaylistdlg.h"

SpeedPlayButton::SpeedPlayButton(QWidget *parent) : QPushButton(parent)
{
    m_width = 32;
    m_height = 32;
    m_pSppedPlayDlg = new CSpeedPlayListDlg(this);
}

void  SpeedPlayButton::setButtonWidth(int tmp_width){
    m_width = tmp_width;
}

void SpeedPlayButton::setButtonHeight(int tmp_height){
    m_height = tmp_height;
}

void SpeedPlayButton::setNormalImage(const QString& normalImage){
    m_normalImage = normalImage;
}

void SpeedPlayButton::paintEvent(QPaintEvent* event){
    QPainter painter(this);
    QPixmap pix;
    pix.load(m_normalImage);
    this->setFixedSize(m_width,m_height);
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    QPen pen(Qt::white);
    painter.setPen(pen);
    painter.drawText(0,0,this->width(),this->height(),Qt::AlignCenter,"倍速");
}

void SpeedPlayButton::enterEvent(QEvent *event){
    showSpeedPlay(event);
    QPushButton::enterEvent(event);
}

void SpeedPlayButton::showSpeedPlay(QEvent *event){
    //获取音量按钮左上角相对于整个界面的位置
    QPoint p1 = this->mapToGlobal(QPoint(0,0));
    QRect btnRect = this->rect();

    //获取滑块对象的矩形区域，rect包含标题栏，去掉标题栏后高度不变
    QRect sliderRect = m_pSppedPlayDlg->rect();

    //计算滑块对象在水平方向上的位置，使其水平居中于声音按钮
    int x = p1.x() + (btnRect.width() - sliderRect.width())/2;

    //计算滑块对象在垂直方向上的位置,使其位于声音按钮上方并略微偏移5px
    int y = p1.y() - sliderRect.height() - 5;

    //滑块对象移动到计算得到的位置
    m_pSppedPlayDlg->move(x,y);

    m_pSppedPlayDlg->show();

}
